package com.bizark.rpc.core.service.region;

import com.alibaba.dubbo.container.page.Page;
import com.alibaba.dubbo.container.page.PageHandler;
import com.bizark.rpc.api.bean.region.Region;
import com.bizark.rpc.api.exception.RpcError;
import com.bizark.rpc.api.exception.RpcException;
import com.bizark.rpc.core.mapper.region.RegionMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by tangshijun on 2017/3/15.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RegionService {

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 根据地区ID查询地区
     *
     * @param id
     * @return
     */
    public Region getRegionById(String id) {
        Region region = regionMapper.selectByPrimaryKey(Integer.valueOf(id));
        return region;
    }

    /**
     * 新增
     * @param region
     * @return
     */
    public Region addRegion(Region region) throws RpcException {

        if(null !=regionMapper.selectByPrimaryKey(Integer.valueOf(region.getId()) )){
            throw new RpcException (RpcError.DATE_ISEXIST_ERROR);
        }
        regionMapper.insertSelective(region);
        //查询
        //、、
        return region;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int deleteRegion(String id) {
        int i = regionMapper.deleteByPrimaryKey(Integer.valueOf(id));
        return i;
    }

    /**
     * 单个更新
     * @param region
     */
    public void updateRegion(Region region) throws RpcException{

        regionMapper.updateByPrimaryKey(region);


    }



    public  void getRegionsPage(Region region){
        PageHelper.startPage(1,0);

        List<Region> rss= regionMapper.selectByPage(region);
        System.out.println(rss);

    }


}
